import Mock from 'mockjs'
import globalInterface from '@/common/global'
// Mock.setup({
//   'headers': {
//     "Content-Type":"text/html;charset=gbk"
//   }
// })
Mock.setup({ timeout: '200-300' })
/**
 * 两种角色 admin和public，这里是根据登录账号来判断是否是admin
 */
export default {
  login (config: globalInterface.IObject) {
    const { url } = config
    const params = url.split('?')[1]
    const usernameparam = params.split('&')[0]
    let name = usernameparam.split('=')[1]
    name = decodeURI(name)
    let data = null
    if (name === 'admin') {
      data = Mock.mock({
        name: name,
        token: 'admin'
      })
    } else {
      data = Mock.mock({
        name: name,
        token: 'public'
      })
    }
    console.log(data)
    return {
      // status: 200,
      code: 'success',
      data,
      messag: '登录成功'
    }
  },
  getUserInfo (config: globalInterface.IObject) {
    const { url } = config
    const params = url.split('?')[1]
    const token = params.split('=')[1]
    console.log(config)
    if (token) {
      let data = null
      if (token === 'admin') {
        data = Mock.mock({
          avatar: Mock.Random.image('50x50'),
          roles: ['admin']
        })
      } else {
        data = Mock.mock({
          avatar: Mock.Random.image('50x50'),
          roles: ['public']
        })
      }
      return {
        code: 'success',
        data,
        messag: '获取用户信息成功'
      }
    } else {
      return {
        code: 'fail',
        message: '获取用户信息失败'
      }
    }
  }
}
